package toy.keli.edic.data.file

import toy.keli.edic.util.FileUtil
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date

interface Base {
    fun file():String;
    val fileName:String
        get(){
            var name = file()
            if(!name.startsWith("/")){
                name = FileUtil.rootDir()+"/"+name
            }

            val parent = File(name).parent
            if(!FileUtil.exists(parent)){
                FileUtil.mkdir(parent)
            }
            return name
        }

    fun date():String{
        val dateFormatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
        val dateNow = Date(System.currentTimeMillis())
        return dateFormatter.format(dateNow)
    }

    fun append(vararg cols:String){
        FileUtil.put(fileName,cols.joinToString("\t")+ "\n" ,true)
    }

    fun gets(): List<String> {
        val f = File(fileName)
        return if(f.exists()) f.readText().split("\n") else listOf<String>()
    }
}